<?php


namespace app\wechat\controller;


use app\common\controller\WechatUserBase;
use app\common\model\Region;
use app\common\model\UserAddress;
use app\common\model\UserCash;
use app\common\model\UserHarvestAddress;
use think\Db;

class User extends WechatUserBase
{
    /**
     * 获取收货地址列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHarvestAddress(){
        $data = UserHarvestAddress::where('user_id','=',session('wechat.id'))->select();
        foreach ($data as $k=>&$v){
            $v['isDefault'] = is_true($v['isDefault']);
            $v['address'] = $v['province'].$v['city'].$v['county'].$v['addressDetail'];
        }
        return $this->result($data);
    }

    /**
     * 获取默认地址
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDefaultUserAddress(){
        $data = UserHarvestAddress::where('user_id','=',session('wechat.id'))->where('isDefault','=','true')->find();
        if (!empty($data)){
            $data = $data->toArray();
        }
        return $this->result($data);
    }

    /**
     * 获取省市区列表
     */
    public function getAreaList(){
        $data = Region::getVantAreaList();
        return $this->result($data);
    }

    public function address_index(){
        return view('user/address/index');
    }
    public function address_create(){
        $id = input('id');
        $this->assign('id',$id);
        return view('user/address/create');
    }

    //    保存与用户地址
    public function saveHarvestAddress(){
        $param = input();
        $param['user_id'] = session('wechat.id');

        if (empty($param['isDefault'])){
            $param['isDefault'] = 'false';
        }else{
            $param['isDefault'] = (string) $param['isDefault'];
        }

        //            如果当前设置为默认，那么其他的都设置不默认
        $UserHarvestAddress = new UserHarvestAddress();
        if ($param['isDefault'] == 'true'){
            $UserHarvestAddress->isUpdate(true)->save(['isDefault'=>'false'],['user_id'=>$param['user_id']]);
        }

        if (isset($param['id']) && is_numeric($param['id'])){
            $UserHarvestAddress->allowField(true)->isUpdate(true)->save($param,['id'=>$param['id']]);
        }else{
            unset($param['id']);
            $UserHarvestAddress->allowField(true)->isUpdate(false)->save($param);
        }
        $this->success();
    }

    /**
     * 获取用户地址详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAddressInfo(){
        $id = input('id');
        $data = UserHarvestAddress::where('id',$id)->find();
        if (!empty($data)){
            $data = $data->toArray();
            $data['isDefault'] = is_true($data['isDefault']);
        }
        return $this->result($data);
    }

    /**
     * 获取用户信息
     */
    public function getUser(){
        $user_id = session('wechat.id');
        $data = \app\common\model\User::getUserInfo($user_id);
        return $this->result($data);
    }

    public function order_list(){
        return view('user/order/index');
    }
    public function order_detail(){
        return view('user/order/detail');
    }
    public function cash_index(){
        return view('user/cash/index');
    }
    public function forum_index(){
        return view('user/forum/index');
    }
    //    删除用户地址
    public function userAddressDel(){
        $id = input('id');
        $res = UserHarvestAddress::destroy($id);
        if ($res !== false){
            $this->success();
        }else{
            $this->error();
        }
    }

    /**
     * 获取地址编码
     */
    public function getAddressCode(){
        $name = input('name');
        $code = Db::name('region')->where('cityName',$name)->value('id');
        $this->result($code);
    }

    //    提现申请
    public function cash(){
        $param = input();
        $res = UserCash::create($param);
        if ($res !== false){
            $this->success();
        }else{
            $this->error();
        }
    }
}
